package org.kimoji.operate.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.kimoji.base.BaseController;
import org.kimoji.entity.Operate;
import org.kimoji.entity.biz.OperateBiz;
import org.kimoji.operate.service.OperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OperateController extends BaseController{

	@Autowired
	private OperateService operateService;
	
	@RequestMapping(value="correction")
	public String correction(){
		return "correction";
	}
	
	/**
	 * 糾錯
	 * <strong>Description</strong>:
	 * @param id
	 * @param reason
	 * @return
	 * @return Object
	 * @author chok
	 * 2014-2-3
	 */
	@RequestMapping(value="operate/correction")
	@ResponseBody
	public Object correction(String id,String reason){
		return null;
	}
	
	
	/**
	 * <strong>Description</strong>:
	 * @param type collect:收藏;support:顶一个;attention:关注
	 * @param id
	 * @param operate
	 * @return
	 * @return Object
	 * @author carlos.xie
	 * 2013-12-8
	 */
	@RequestMapping(value="operate/{type}/{targetId}/{action}")
	@ResponseBody
	public Object operate(@PathVariable String type,@PathVariable String targetId,@PathVariable String action,HttpSession session,@CookieValue(value="token",defaultValue="")Cookie token){
		OperateBiz operation = new OperateBiz();
		Operate operate = new Operate();
		operate.setAction(action);
		operate.setType(type);
		operate.setTargetId(targetId);
		String id = getAuth(session, token);
			if(type.equals("collect")){//收藏
				operateService.collect(id,operate);
				operation.setIsCollect("accept".equals(action));
			}else if(type.equals("support")){//點贊
				operateService.support(id,operate);
				operation.setNum("accept".equals(action)?1:-1);
			}else if(type.equals("attention")){//關注
				operateService.attent(id,operate);
				operation.setIsAttention("accept".equals(action));
			}
		operation.setSuccess(Boolean.TRUE);
		return operation;
	}
	
}
